package com.example.liumuyao.widgettest_412lmy;

import android.content.Intent;
import android.inputmethodservice.KeyboardView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView TV;
    private EditText ET;
    private Button btngettext,btnsetimg;
    private ImageView IV;
    private ImageButton IB;
    //声明8个控件
    private RadioGroup rg;
    private RadioButton rbnan,rbnv;
    private CheckBox cblan,cbzu,cbyou;
    private Button btngetgender,btngetfun;

    private String strfun="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        //获取控件
        TV=(TextView)findViewById(R.id.textView3);
        ET=(EditText) findViewById(R.id.editText);
        btngettext=(Button) findViewById(R.id.button);
        btnsetimg=(Button)findViewById(R.id.button2);
        IV=(ImageView) findViewById(R.id.imageView);
        IB=(ImageButton) findViewById(R.id.imageButton3);
        //设置监听器
        btnsetimg.setOnClickListener(this);
        btngettext.setOnClickListener(this);
        IB.setOnClickListener(this);

        rg = (RadioGroup)findViewById(R.id.radiogroup);
        rbnan=(RadioButton)findViewById(R.id.radioButton);
        rbnv=(RadioButton)findViewById(R.id.radioButton2);
        cblan=(CheckBox)findViewById(R.id.checkBox2);
        cbzu=(CheckBox)findViewById(R.id.checkBox3);
        cbyou=(CheckBox)findViewById(R.id.checkBox4);
        btngetgender=(Button)findViewById(R.id.button4);
        btngetfun=(Button)findViewById(R.id.button5);

        btngetgender.setOnClickListener(this);
        btngetfun.setOnClickListener(this);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            public void onCheckedChanged(RadioGroup group,int checkedId) {
                switch (checkedId) {
                    case R.id.radioButton:
                        TV.setText("男");
                        break;
                    case R.id.radioButton2:
                        TV.setText("女");
                        break;
                }
            }
        });
        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    strfun+="篮球+";
                else
                    strfun=strfun.replace("篮球+","");
                TV.setText(strfun);
            }
        });
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    strfun+="足球+";
                else
                    strfun=strfun.replace("足球+","");
                TV.setText(strfun);
            }
        });
        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    strfun+="游泳+";
                else
                    strfun=strfun.replace("游泳+","");
                TV.setText(strfun);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button2://设置文本
                String str=ET.getText().toString();
                TV.setText(str);
                break;
            case R.id.button://设置图片
                IV.setImageResource(R.drawable.lmy2);
                break;
            case R.id.imageButton3://图片按钮
                Intent intent = new Intent(MainActivity.this,NewActivity.class);
                intent.putExtra("param1",ET.getText().toString());
                intent.putExtra("param2",3);
                startActivity(intent);
                break;
            case  R.id.button4://获取性别
                if (rbnan.isChecked())  TV.setText("男");
                if (rbnv.isChecked())  TV.setText("女");
                break;
            case  R.id.button5://获取兴趣
                String msg="";
                if (cblan.isChecked()) msg+="篮球+";
                if (cbzu.isChecked())  msg+="足球+";
                if ((cbyou.isChecked())) msg+="游泳";
                TV.setText(msg);
                break;
        }
    }
}
